package com.epam.edu.command;

import com.epam.edu.constant.*;
import com.epam.edu.dao.*;
import com.epam.edu.db.ConnectionPool;
import com.epam.edu.entity.*;
import com.epam.edu.util.CryptoUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

public class LoginCommand implements Command {
    @Override
    public String perform(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String page = null;
        HttpSession session = request.getSession();
        String login = request.getParameter("userName");
        String password = CryptoUtils.SHA1(request.getParameter("password"));
        ConnectionPool pool = null;
        Connection connection = null;
        try {
            ConnectionPool.init();
            pool = ConnectionPool.getInstance();
            connection = pool.takeConnection();
            User user = new User(login, password);
            if (DAOUser.getUserID(connection,user) != -1) {
                session.setAttribute("userId", user.getId());
                session.setAttribute("userRole", user.getRole());
                Cookie getUserId = new Cookie("userId", user.getIdAsString());
                response.addCookie(getUserId);
                List<Nationality> nationalities = DAONationality.selectAll(connection);
                request.setAttribute("nationality", nationalities);
                List<Citizenship> citizenship = DAOCitizenship.selectAll(connection);
                request.setAttribute("citizenship", citizenship);
                List<DocumentType> docTypes = DAODocumentType.selectAll(connection);
                request.setAttribute("docTypes", docTypes);
                List<Department> departments = DAODepartment.selectAll(connection);
                request.setAttribute("departments", departments);
                List<EducationType> educationTypes = DAOEducationType.selectAll(connection);
                request.setAttribute("educationTypes", educationTypes);
                List<Specialty> specialties = DAOSpecialty.selectAll(connection);
                request.setAttribute("specialties", specialties);
                List<TeachingForm> teachingForms = DAOTeachingForm.selectAll(connection);
                request.setAttribute("teachingForms", teachingForms);
                List<TeachingPlan> teachingPlans = DAOTeachingPlan.selectAll(connection);
                request.setAttribute("teachingPlans", teachingPlans);
                List<String> languages = new LinkedList<String>();
                for (Language lang : Language.values()) {
                    languages.add(lang.toString());
                }
                request.setAttribute("languages", languages);
                List<String> paymentTypes = new LinkedList<String>();
                for (PaymentType pay : PaymentType.values()) {
                    paymentTypes.add(pay.toString());
                }
                request.setAttribute("paymentTypes", paymentTypes);
                List<String> sexs = new LinkedList<String>();
                for (Sex sex : Sex.values()) {
                    sexs.add(sex.toString());
                }
                request.setAttribute("sexs", sexs);
                List<String> familyStatues = new LinkedList<String>();
                for (FamilyStatus familyStatus : FamilyStatus.values()) {
                    familyStatues.add(familyStatus.toString());
                }
                request.setAttribute("familyStatues", familyStatues);
                page = "/WEB-INF/jsp/admin.jsp";
            } else {
                request.setAttribute("errorMessage", "Invalid login or password");
                page = "/WEB-INF/jsp/login.jsp";
            }
            pool.releaseConnection(connection);
            ConnectionPool.dispose();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return page;
    }
}
